package edu.xidian.onlinedocument.repository;

import edu.xidian.onlinedocument.entity.*;

import org.springframework.data.jpa.repository.*;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
public interface UserDataJPARepository extends JpaRepository<UserData, Integer>, JpaSpecificationExecutor<UserData> {

    boolean existsUserDataByUserId(int userId);

    boolean existsUserDataByUserNum(String userNum);

    boolean existsUserDataByUserToken_UserToken(String userToken);

    List<UserData> findAllByUserId(int userId);

    List<UserData> findAllByUserNum(String userNum);

    /**
     * 查询一名用户的信息（根据user_num，即账号）
     * @param userNum 账号
     * @return UserData
     */
    // @Query(nativeQuery = true, value = "select * from user_data where user_num = :user_num")
    UserData findUserDataByUserNum(/*@Param("user_num") */String userNum);

    /**
     * 查询一名用户的信息（根据user_id）
     * @param userId 用户id
     * @return UserData
     */
    UserData findUserDataByUserId(int userId);

    /**
     * 查询一名用户的信息（根据token）
     * @param userToken token
     * @return UserData
     */
    /*@Query(nativeQuery = true, value =
            "select * from user_data " +
            "where user_data.user_id = " +
            "(select user_id from user_id_token where user_token = :user_token)")*/
    UserData findUserDataByUserToken_UserToken(/*@Param("user_token") */String userToken);

    /**
     * 根据用户账号查找用户id
     * @param userNum 用户账号
     * @return userId
     */
    @Query(nativeQuery = true,
           value = "select user_id from user_data where user_num = :user_num")
    Integer findUserIdByUserNum(@Param("user_num") String userNum);

    /**
     * 根据用户token查找用户id
     * @param userToken token
     * @return userId
     */
    @Query(nativeQuery = true,
            value = "select user_id from user_data where user_id = " +
                    "(select user_id from user_id_token where user_token = :user_token)")
    Integer findUserIdByUserToken(@Param("user_token") String userToken);

    /**
     * 验证用户的账号名和密码信息是否正确，若正确，则返回非空值
     * @param userNum 用户账号
     * @param userKey 用户密码
     * @return UserData
     */
    // @Query(nativeQuery = true, value = "select * from user_data where user_num = :u_num AND user_key = :u_key")
    UserData findUserDataByUserNumAndUserKey(/*@Param("user_num") */String userNum,
            /*@Param("user_key") */String userKey);

    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    @Modifying
    @Query(nativeQuery = true, value = "insert into user_data(user_name, user_num, user_key) "
                                     + "values(:user_name, :user_num, :user_key)"
    )
    int addUserData(@Param("user_name") String userName,
                     @Param("user_num") String userNum,
                     @Param("user_key") String userKey);

    /**
     * 根据用户账号删除一名用户
     * @param userNum 用户账号
     * @return 删除的行数，若失败返回<0
     */
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    @Modifying
    // @Query(nativeQuery = true, value = "delete from user_data where user_num = :u_num ")
    int deleteUserDataByUserNum(/*@Param("user_num") */String userNum);

    /**
     * 根据用户id删除一名用户
     * @param userId 用户id
     * @return 删除的行数，若失败返回<0
     */
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    @Modifying
        // @Query(nativeQuery = true, value = "delete from user_data where user_num = :u_num ")
    int deleteUserDataByUserId(/*@Param("user_num") */String userId);
}
